using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace logPuzzle
{
    public partial class Form1 : Form
    {
        private int m_C = 0;
        private int m_R = 0;
        private int m_CN = 0;
        private int m_RN = 0;
        private DataTable m_DTPuzzle = null;
        private DataTable m_DTC = null;
        private DataTable m_DTR = null;
        private PManager m_Manager = null;

        public Form1()
        {
            InitializeComponent();

            this.dgvPuzzle.AutoGenerateColumns = true;
            this.dgvC.AutoGenerateColumns = true;
            this.dgvR.AutoGenerateColumns = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.m_C = int.Parse(this.tbC.Text);
                this.m_R = int.Parse(this.tbR.Text);

                this.m_CN = int.Parse(this.tbCN.Text);
                this.m_RN = int.Parse(this.tbRN.Text);

                this.m_DTPuzzle = new DataTable("Puzzle");
                this.m_DTC = new DataTable("C");
                this.m_DTR = new DataTable("R");

                // column rows
                for (int i = 0; i < this.m_RN; i++)
                {
                    this.m_DTR.Columns.Add(i.ToString(), typeof(int));
                }

                // row rows
                for (int i = 0; i < this.m_R; i++)
                {
                    this.m_DTR.Rows.Add(this.m_DTR.NewRow());
                }

                // column columns
                for (int i = 0; i < this.m_C; i++)
                {
                    this.m_DTC.Columns.Add(i.ToString(), typeof(int));
                }

                // row columns
                for (int i = 0; i < this.m_CN; i++)
                {
                    this.m_DTC.Rows.Add(this.m_DTC.NewRow());
                }

                this.m_Manager = new PManagerBit(this.m_DTR, this.m_DTC, this.m_DTPuzzle);

                this.bsPuzzle.DataSource = this.m_DTPuzzle;
                this.bsC.DataSource = this.m_DTC;
                this.bsR.DataSource = this.m_DTR;
            }
            catch
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.backgroundWorker1.IsBusy) this.backgroundWorker1.CancelAsync();
            else this.backgroundWorker1.RunWorkerAsync();
        }

        /***/

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            this.m_Manager.Clear();
            this.m_Manager.Init();
            this.m_Manager.Generate(this.backgroundWorker1);
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.dgvPuzzle.Refresh();
        }
    }

}